package com.book.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.book.config.RedisKeyConfig;
import com.book.dao.TbBookDao;
import com.book.dao.TbMeDao;
import com.book.entity.TbBook;
import com.book.entity.TbMe;
import com.book.service.TbMeService;
import com.book.vo.R;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static java.util.Collections.list;

/**
 * (TbMe)表服务实现类
 *
 * @author makejava
 * @since 2024-11-07 19:53:31
 */
@Service("tbMeService")
@RequiredArgsConstructor
public class TbMeServiceImpl implements TbMeService {
    private final TbMeDao tbMeDao;
    private final TbBookDao bookDao;
    private final StringRedisTemplate redisTemplate;

    @Override
    public R meAdd(String token, Integer bookId) {
        //判断令牌是否存在
        if (!redisTemplate.hasKey(RedisKeyConfig.USER_TOKEN+token)){
            return R.fail("请先登录!");
        }
        //通过令牌获取用户id
        String uid = redisTemplate.opsForValue().get(RedisKeyConfig.USER_TOKEN+token);

        if (uid==null){
            return R.fail("登录已过期,请重新登录！");
        }
        LambdaQueryWrapper<TbMe> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbMe::getUid,uid);
        wrapper.eq(TbMe::getBid,bookId);
        TbMe tbMe = tbMeDao.selectOne(wrapper);
        if (tbMe==null){
            int i = tbMeDao.insert(new TbMe(Integer.parseInt(uid), bookId));
            if (i>0){
                return R.ok("收藏成功");
            }
        }
        return R.fail("收藏失败!");

    }

    @Override
    public R findMe(String token, Integer bookId) {
        //判断令牌是否存在
        if (!redisTemplate.hasKey(RedisKeyConfig.USER_TOKEN+token)){
            return R.fail("请先登录!");
        }
        //通过令牌获取用户id
        String uid = redisTemplate.opsForValue().get(RedisKeyConfig.USER_TOKEN+token);
        if (uid==null){
            return R.fail("登录已过期,请重新登录！");
        }
        LambdaQueryWrapper<TbMe> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbMe::getUid,uid);
        wrapper.eq(TbMe::getBid,bookId);
        TbMe tbMe = tbMeDao.selectOne(wrapper);
        if (tbMe!=null){
            return R.ok(null);
        }
        return R.fail(null);
    }

    @Override
    public R deleteMe(String token, Integer bookId) {
        //判断令牌是否存在
        if (!redisTemplate.hasKey(RedisKeyConfig.USER_TOKEN+token)){
            return R.fail("请先登录!");
        }
        //通过令牌获取用户id
        String uid = redisTemplate.opsForValue().get(RedisKeyConfig.USER_TOKEN+token);
        if (uid==null){
            return R.fail("登录已过期,请重新登录！");
        }
        LambdaQueryWrapper<TbMe> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbMe::getUid,uid);
        wrapper.eq(TbMe::getBid,bookId);
        TbMe tbMe = tbMeDao.selectOne(wrapper);
        if (tbMe!=null){
            int i = tbMeDao.delete(wrapper);
            if (i>0){
                return R.ok("取消收藏成功!");
            }
        }
        return R.fail("取消收藏失败!");
    }

    @Override
    public R mybook(String token) {
        //判断令牌是否存在
        if (!redisTemplate.hasKey(RedisKeyConfig.USER_TOKEN+token)){
            return R.fail("请先登录!");
        }
        //通过令牌获取用户id
        String uid = redisTemplate.opsForValue().get(RedisKeyConfig.USER_TOKEN+token);
        //判断用户是否为空
        if(uid==null){
            return R.fail("登录已过期,请重新登录！!");
        }

        //如果用户存在，通过用户id获取图书id
        LambdaQueryWrapper<TbMe> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbMe::getUid,uid);
        List<TbMe> list = tbMeDao.selectList(wrapper);
        List<TbBook> bookList = new ArrayList<>();
        for (TbMe tbMe : list) {
            TbBook tbBook = bookDao.selectById(tbMe.getBid());
            bookList.add(tbBook);
        }
            return R.ok(bookList);
    }
}

